require(["jquery",'public','weui'],function($,public,weui) {
    //public.headertitle('个人信息');
    //public.footer(1);
    public.allocator();

    var cates = {
        1: '农副产品',
        2: '生产加工',
        3: '生活用品',
        4: '服装',
        5: '其他',
    };

    function getData(){
    	$.ajax({
            data: {token: public.getSession('token')},
	        type: 'post',
	        dataType: 'json',
	        url: MODULE+'Index/personal',	        
	    }).success(function (res) {
	        console.log(res);
            if(res.code == 0 && res.data && res.data.user_open == 1){
    	        $('[name="user_shop"]').val(res.data.user_shop);
    	        $('[name="user_shop_area"]').val(res.data.user_shop_area);
    	        $('[name="user_cc_id"]').val(res.data.user_cc_id);
    	        $('[name="user_shop_category"]').val(res.data.user_shop_category);
    	        $('[name="user_shop_path"]').val(res.data.user_shop_path);
    	        $('.weui-uploader__input-box').hide();
                $('.product_item_input_cate').addClass('bgnone').data('id',res.data.user_cc_id).text(cates[res.data.user_cc_id]);
    	        $('.choose_box').html('<div class="choose_img_item" style="background-image:url('+PUBLIC+res.data.user_shop_path+')"><a class="choose_img_item_delete" href="#"></a></div>');
            }
	    }).fail(function (jqXHR, textStatus, errorThrown) {});
    }

    getData();

    //页面加载完毕
    public.loaded();

	var tmpl = '<div class="choose_img_item" style="background-image:url(#url#)"><a class="choose_img_item_delete" href="#"></a></div>';
    var $uploaderInput = $(".weui-uploader__input");
    var $gallery = $("#gallery");
    var $galleryImg = $("#galleryImg");
    $uploaderInput.on("change", function(e){
    	var $this = $(this);
    	var $uploaderFiles = $this.parent().siblings('.choose_box');
    	var length = $this.data('length') || 1;
        var src, url = window.URL || window.webkitURL || window.mozURL, files = e.target.files;
        for (var i = 0, len = files.length; i < len; ++i) {
            var file = files[i];
            if(file.type.search(/image/) < 0){
                $.alert('请选择图片');
                return false;
            }
            if (url) {
                src = url.createObjectURL(file);
            } else {
                src = e.target.result;
            }
            $uploaderFiles.append($(tmpl.replace('#url#', src)));
            if($uploaderFiles.find('.choose_img_item').size() >= length){
            	$this.parent().hide();
            	return false;
            }
        }
    });

    //打开分类
    $(document).on('click','.product_item_input_cate',function(event){
        event.preventDefault();
        if($(this).hasClass('on')){
            return false;
        }
        $(this).addClass('on');
        $('.product_cate').removeClass('hide');
        setTimeout(function(){
            $('.product_cate').addClass('show');
        },10);
    });

    //关闭分类
    $('.product_cate').on('click','.backAdd',function(event){
        event.preventDefault();
        event.stopPropagation();
        $('.product_cate').removeClass('show');
        setTimeout(function(){
            $('.product_item_input_cate').removeClass('on');
            $('.product_cate').addClass('hide');
        },400);
    });
    $('.product_cate').on('click',function(event){
        if($(event.target).hasClass('product_cate')){
            $('.backAdd').trigger('click');
        }
    });

    //选择分类
    $('.product_cate').on('click','.cate_item',function(event){
        event.preventDefault();
        event.stopPropagation();
        var id = $(this).data('id');
        var txt = $(this).text();
        $('.product_item_input_cate').addClass('bgnone').html(txt).attr('data-id',id);
        $('[name="user_cc_id"]').val(id);
        $('[name="user_shop_category"]').val(txt);
        $('.backAdd').trigger('click');
    });

    $('.choose_box').on('click','.choose_img_item_delete',function(event){
    	event.preventDefault();
    	event.stopPropagation();
    	var $this = $(this);
    	$this.closest('.choose_box').siblings('.weui-uploader__input-box').show();
    	$this.parent().remove();
    });

    $('.choose_box').on("click", ".choose_img_item", function(){
        $galleryImg.attr("style", this.getAttribute("style"));
        $gallery.fadeIn(100);
    });
    $gallery.on("click", function(){
        $gallery.fadeOut(100);
    });


    $('.save_box').on('click','.save',function(event){
    	event.preventDefault();
        var user_shop = $('[name="user_shop"]').val().trim();
        var user_shop_area = $('[name="user_shop_area"]').val().trim();
        var user_cc_id = $('[name="user_cc_id"]').val();
        console.log(user_shop)
        console.log(user_shop_area)
        console.log(user_cc_id)
        if(user_shop != '' && user_shop_area !='' && user_cc_id != ''){
    	   $('#upload_form').append('<input class="hide" name="token" value="'+public.getSession('token')+'">').submit();
        }else{
            $.alert('信息不完整');
        }
    });

    $('#upload_frame_1').unbind("load").load(function() {
        $.alert("提交成功",function(){
            window.location.replace(MODULE + 'PublicJump/personal');
        });
		/*var contents = $(this).contents().get(0);
		var data = $(contents).find('body').text();
        console.log(data);
		try{
            data = window.eval('(' + data + ')');
        }catch(e){
            if(data != ''){
                $.alert("修改失败");
            }
        }
        if(data == ''){
            $.confirm("你要离开本页么?", function() {
                window.history.go(-1);
            }, function() {
                //...
            });
            return false;
        }
        console.log(data);
        if(data.code == 0){
            $.alert("修改成功");
        }else if(data.code == 1){
            $.alert("修改失败");
        }*/
	});

})